package com.zfy.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zfy.tingshu.album.mapper.*;
import com.zfy.tingshu.album.service.BaseCategoryService;
import com.zfy.tingshu.model.album.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {
		//创建集合对象
		ArrayList<JSONObject> list = new ArrayList<>();
		//数据来源于数据库：base_category1,base_category2,base_category3
		//使用视图获取数据
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//json数据是通过map map.put(key,value)或class setName("zs")形式！{"name","zs"}
		//使用lambda表达式进行分组
		//key =categary1Id value =List<BaseCategoryView>
		Map<Long, List<BaseCategoryView>> baseCategory1Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = baseCategory1Map.entrySet().iterator();
		while (iterator.hasNext()){
			//获取entry对象
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			//获取数据
			Long category1Id = entry.getKey();
			List<BaseCategoryView> categoryViewList = entry.getValue();
			//JSONObject extends JSON implements Map<String,Object>
			JSONObject category1 = new JSONObject();
			category1.put("categoryName",categoryViewList.get(0).getCategory1Name());
			category1.put("categoryId",category1Id);
			//获取一级分类下的二级分类数据
			Map<Long, List<BaseCategoryView>> Category2Map = categoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = Category2Map.entrySet().iterator();
			//存储2级分类数据
			ArrayList<JSONObject> categoryChild2List = new ArrayList<>();
			while (iterator1.hasNext()) {
				//获取2级分类数据，音乐下面应该有3次循环
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				Long category2Id = entry1.getKey();
				List<BaseCategoryView> categoryViewList1 = entry1.getValue();
				JSONObject category2 = new JSONObject();
				category2.put("categoryName",categoryViewList1.get(0).getCategory1Name());
				category2.put("categoryId",category2Id);
				//获取3级分类数据，直接循环遍历3级分类数据即可
				List<JSONObject> categoryChild3List = categoryViewList1.stream().map(baseCategoryView -> {
					//创建3级分类对象
					JSONObject category3 = new JSONObject();
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					//返回数据
					return category3;
				}).collect(Collectors.toList());
				//获取3级分类数据添加到2级分类中
				category2.put("categoryChild",categoryChild3List);
				categoryChild2List.add(category2);
			}
			//将2级分类数据添加到1级分类数据中
			category1.put("categoryChild",categoryChild2List);
			list.add(category1);
		}
		return list;

	}

	@Override
	public List<BaseAttribute> findAttribute(Integer category1Id) {
		return baseAttributeMapper.findAttribute(category1Id);

	}

	@Override
	public BaseCategoryView getBaseCategoryView(Long category3Id) {
		BaseCategoryView baseCategoryView = this.baseCategoryViewMapper.selectOne(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory3Id, category3Id));
		return baseCategoryView;
	}

	@Override
	public JSONObject getBaseCategory1List(Long category1Id) {

		JSONObject jsonObject = new JSONObject();

		//根据一级分类获取信息
		BaseCategory1 category1 = baseCategory1Mapper.selectById(category1Id);

		jsonObject.put("categoryId",category1Id);
		//获取全部分类信息
		List<BaseCategoryView> baseCategoryViewList =
				baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));


		jsonObject.put("categoryName",baseCategoryViewList.get(0).getCategory1Name());
		//根据二级分类ID分组转换数据
		Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		ArrayList<JSONObject> category2Child = new ArrayList<>();
		category2Map.forEach( (k,v) ->{
			JSONObject jsonObject2 = new JSONObject();
			//二级分类ID
			jsonObject2.put("categoryId",k);
			jsonObject2.put("categoryName",v.get(0).getCategory2Name());

			//获取三级分类ID

			ArrayList<JSONObject> category3Child = new ArrayList<>();
			v.forEach(t->{
				JSONObject jsonObject3 = new JSONObject();
				jsonObject3.put("categoryId",t.getCategory3Id());
				jsonObject3.put("categoryName",t.getCategory3Name());
				category3Child.add(jsonObject3);
			});
			jsonObject2.put("categoryChild",category3Child);
			category2Child.add(jsonObject2);
		});
		jsonObject.put("categoryChild",category2Child);


		return jsonObject;
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//	select * from base_category3 where base_category3.category2_id in (101,102,103) and is_top = 1 limit 7;
		//先根据一级分类Id 找到二级分类集合
		LambdaQueryWrapper<BaseCategory2> baseCategory2LambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategory2LambdaQueryWrapper.eq(BaseCategory2::getCategory1Id,category1Id);
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(baseCategory2LambdaQueryWrapper);
		List<Long> category2IdList = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		//查询置顶消息，每页显式7条数据
		LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategory3LambdaQueryWrapper.in(BaseCategory3::getCategory2Id,category2IdList)
				.eq(BaseCategory3::getIsTop,1)
				.last(" limit 7");
		//

		return baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);
	}
}
